<template>
    <div class="app-box">
        <div class="title">
            <div class="system-name">假装是个LOGO</div>
            <!-- 上方菜单 -->
            <div class="menu-box">
                <div class="flex menu" v-for="(menu, index) in menuList"
                    :class="menu.id == currentMenuTab.id ? 'menu-selection' : ''">
                    <div class="menu menu-inner" v-if="index === 0 || menu.children?.length === 0"
                        @click="changeMenu(menu)">
                        <div class="menu-icon">
                            <div class="menu-icon-box"
                                :style="{ backgroundImage: `url(${rq.image(menu.icon as string)})` }">
                            </div>
                        </div>
                        <div class="menu-name">
                            {{ menu.label }}
                        </div>
                        <div class="h"></div>
                    </div>
                    <el-dropdown trigger="click" v-else ref="menuDropDown">
                        <div class="menu menu-inner">
                            <div class="menu-icon">
                                <div class="menu-icon-box"
                                    :style="{ backgroundImage: `url(${rq.image(menu.icon as string)})` }">
                                </div>
                            </div>
                            <div class="menu-name">
                                {{ currentMenuTab.label == '首页' ? menu.label : currentMenuTab.label }}
                            </div>
                            <div class="h"></div>
                        </div>
                        <template #dropdown>
                            <div class="app-child">
                                <menu-item v-for="item in menu.children" @on-click="changeMenu" :key="item.id" :data="item"
                                    :parentId="menu.id ?? ''"></menu-item>
                            </div>
                        </template>
                    </el-dropdown>
                </div>
            </div>
            <div class="title-info">
                <div class="wrap flex">
                    <!-- 消息提示 -->
                    <div class="title-info-tip">
                        <div class="title-info-tip-message"></div>
                    </div>
                    <!-- 用户信息展示 -->
                    <el-dropdown trigger="click">
                        <div class="title-info-user">
                            <!-- 头像 -->
                            <div class="title-info-user-title"></div>
                            <!-- 用户名 -->
                            <div class="title-info-user-name">{{ userInfo.username }}</div>
                            <!-- 下拉选 -->
                            <div class="title-info-user-drop-down"></div>
                        </div>
                        <template #dropdown>
                            <el-dropdown-menu>
                                <el-dropdown-item :icon="Tools">设置</el-dropdown-item>
                                <el-dropdown-item :icon="SwitchButton" @click="logout">退出</el-dropdown-item>
                            </el-dropdown-menu>
                        </template>
                    </el-dropdown>

                </div>
            </div>
        </div>
        <main class="content">
            <div id="contentMenu" class="content-menu">
                <div class="wrap">
                    <el-menu ref = "menuObj" default-active="1" class="el-menu-vertical-demo" :unique-opened=true
                        active-text-color="#2561ef" background-color="#fff" text-color="#000" :collapse-transition=false>
                        <template v-for="item in currentMenuTab.children" :key="item.id">
                            <el-sub-menu v-if="item.hasChild" :index="item.id">
                                <template #title>
                                    <el-icon>
                                        <div v-if="item.icon" class="content-menu-item-icon"
                                            :style="{ backgroundImage: `url(${rq.image(item.icon)})` }">
                                        </div>
                                    </el-icon>
                                    <span>{{ item.label }}</span>
                                </template>
                                <el-menu-item v-for="subItem in item.children" :key="subItem.id" :index="subItem.id"
                                    @click="selectPage(subItem)">
                                    <el-icon v-if="subItem.icon">
                                        <div class="content-menu-item-icon"
                                            :style="{ backgroundImage: `url(${rq.image(subItem.icon)})` }">
                                        </div>
                                    </el-icon>
                                    <span>{{ subItem.label }}</span>
                                </el-menu-item>
                            </el-sub-menu>
                            <el-menu-item v-else :index="item.id" @click="selectPage(item)">
                                <el-icon v-if="item.icon">
                                    <div class="content-menu-item-icon"
                                        :style="{ backgroundImage: `url(${rq.image(item.icon)})` }">
                                    </div>
                                </el-icon>
                                <span>{{ item.label }}</span>
                            </el-menu-item>
                        </template>
                    </el-menu>
                </div>
            </div>
            <div class="content-content">
                <div class="wrap flex cw">
                    <div class="content-content-title">
                        <el-breadcrumb :separator-icon="ArrowRight">
                            <el-breadcrumb-item v-for="name in breadcrumbItems">{{ name }}</el-breadcrumb-item>
                        </el-breadcrumb>
                    </div>
                    <div class="content-content-content">
                        <RouterView></RouterView>
                    </div>
                    <div class="content-content-foot">底部预留区域</div>
                </div>
            </div>
        </main>
    </div>
</template>

<script setup lang="ts">
import type { MenuType } from '@/assets/js/portal/MenuType'
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { CacheUserUtil } from '@/assets/js/utils/CacheUserUtil'
import { Require } from '@/assets/js/utils/Require'
import { Tools, SwitchButton, ArrowRight } from '@element-plus/icons-vue'
import MenuItem from './components/MenuItem.vue'
import { menuItemList } from '@/api/portal/index'
import type { HttpResponse } from '@/assets/js/utils/entity/HttpResponse'

const userInfo = reactive(CacheUserUtil.getUserInfo())

const router = useRouter()

const rq = new Require()

const menuDropDown = ref()

let menuList = reactive<Array<MenuType>>([])
let menuListData: Array<MenuType> = []
let currentMenuTab = reactive<MenuType>({})
const menuObj = ref()

// 面包屑
let breadcrumbItems = reactive<string[]>([])

const getMenuList = async () => {
    const res: HttpResponse<Array<MenuType>> = await menuItemList() as unknown as HttpResponse<Array<MenuType>>
    if (res.ok) {
        res.data?.forEach(item => {
            menuList.push(item)
            menuListData.push(item)
        })
        changeMenu(menuList[0])
    }
}
getMenuList()

// 选择菜单事件
const selectPage = (item: MenuType) => {
    changeMenu(item, currentMenuTab.id, true)
}

// 退出事件
const logout = () => {
    CacheUserUtil.clear()
    // 返回到登录页
    router.push('/')
}

// 点击某个应用的事件
const changeMenu = (val: MenuType, id: string = '', onlyTo: boolean = false) => {
    let tmp = null
    if (!onlyTo) {
        currentMenuTab.id = id || val.id
        currentMenuTab.realId = val.realId
        currentMenuTab.label = val.label
        currentMenuTab.hasChild = val.hasChild
        currentMenuTab.children = val.children
        currentMenuTab.icon = val.icon
        currentMenuTab.select = val.select
        currentMenuTab.url = val.url
        currentMenuTab.path = val.path
        currentMenuTab.value = val.value
        menuDropDown?.value && menuDropDown?.value[0]?.handleClose()

        // 跳转路由
        let path = currentMenuTab.url
        tmp = currentMenuTab
        let index = 0
        while (!path && index < 10) {
            index++
            if (tmp.children?.length) {
                path = tmp.children[0].url
                tmp = tmp.children[0]
            }
        }
        if (path) {
            router.push(path)
        }
    } else {
        tmp = val
        router.push(val.url!)
    }

    // 处理面包屑
    breadcrumbItems.splice(0, breadcrumbItems.length)
    const arr = tmp.path?.split('/') ?? []
    for (let i = 0; i < arr.length; i++) {
        breadcrumbItems.push(arr[i])
    }
}

const getNameById = (id: string, list: MenuType[], str: string[]) => {
    for (let i = 0; i < list.length; i++) {
        if (list[i].id === id) str.push(list[i].label!)
    }
    for (let i = 0; i < list.length; i++) {
        if (list[i].hasChild) {
            getNameById(id, list[i].children!, str)
        }
    }
}

</script>

<style lang="less" scoped>
.wrap {
    width: 100%;
    height: 100%;
    position: relative;
}

.flex {
    display: flex;
}

.app-box {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;

    .title {
        width: 100%;
        height: 60px;
        min-height: 60px;
        background-color: rgb(37, 97, 239);
        display: flex;
        position: relative;

        .system-name {
            width: auto;
            height: 100%;
            padding-left: 20px;
            padding-right: 20px;
            display: inline-flex;
            align-items: center;
            font-size: 16px;
            color: #fff;
        }

        &-info {
            position: absolute;
            right: 0;
            top: 0;
            width: auto;
            height: 100%;

            &-tip {
                width: 35px;
                height: 100%;
                display: inline-flex;
                justify-content: center;
                align-items: center;

                &-message {
                    width: 14px;
                    height: 18px;
                    background: url("@/assets/images/portal/message.png") no-repeat 100% 100%;
                    cursor: pointer;
                }
            }

            &-user {
                padding-left: 10px;
                padding-right: 20px;
                height: 100%;
                display: inline-flex;
                align-items: center;

                &-title {
                    width: 40px;
                    height: 40px;
                    border-radius: 4px;
                    background-image: url("@/assets/images/portal/u945.png");
                    background-repeat: no-repeat;
                    background-size: 100% 100%;
                    cursor: pointer;
                }

                &-name {
                    padding-left: 15px;
                    padding-right: 15px;
                    height: 100%;
                    display: inline-flex;
                    align-items: center;
                    color: #fff;
                    cursor: pointer;
                }

                &-drop-down {
                    width: 9px;
                    height: 6px;
                    background-image: url("@/assets/images/portal/drop-down.png");
                    background-repeat: no-repeat;
                    background-size: 100% 100%;
                    cursor: pointer;
                }
            }
        }

        .menu-box {
            margin-left: 25px;
            height: 100%;
            display: inline-flex;

            .menu {
                width: 100px;
                margin-right: 20px;
                height: 100%;
                position: relative;

                &-icon,
                &-name {
                    display: flex;
                    width: 100%;
                    height: 50%;
                    justify-content: center;
                    align-items: center;
                    font-size: 16px;
                    color: #fff;
                    cursor: pointer;
                }

                &-icon-box {
                    width: 15px;
                    height: 15px;
                    background-repeat: no-repeat;
                    background-size: 100% 100%;
                }

                &-name {
                    align-items: baseline;
                }

                .h {
                    position: absolute;
                    bottom: 0;
                    left: 0;
                    width: 100%;
                    height: 3px;
                    background-color: #fff;
                    display: none;
                }

                &-selection {
                    background-color: rgb(19, 74, 222);
                    cursor: default;
                }

                &-selection .h {
                    display: block;
                }
            }

            .menu-inner {
                margin: 0;
            }

            .menu:hover {
                background-color: rgb(19, 74, 222);
                cursor: default;
            }

            .menu:hover>.h {
                display: block;
            }
        }
    }

    .content {
        flex: 1;
        position: relative;
        flex-direction: row;
        display: flex;

        &-menu {
            height: 100%;
            width: 200px;
        }

        &-content {
            height: 100%;
            background-color: rgb(242, 243, 245);
            flex: 1;

            .cw {
                padding: 10px 15px 10px 15px;
                flex-direction: column;
                width: calc(100% - 30px);
                height: calc(100% - 20px);
            }

            &-title {
                width: 100%;
                height: 30px;
                display: flex;
                align-items: center;
            }

            &-content {
                flex: 1;
            }

            &-foot {
                width: 100%;
                height: 30px;
            }
        }
    }

}

.content-menu-item-icon {
    background-repeat: no-repeat;
    background-size: 100% 100%;
    width: 100%;
    height: 100%;
}

.el-menu {
    border-right: 0;
}

.app-child {
    padding: 10px;
    max-width: 500px;
}
</style>